package wangjg.security.deprecated;

import java.util.Iterator;

import wangjg.security.core.ResourceFactory;
import wangjg.security.core.validator.RequestValidator;



/**
 * 验证指定的[用户]是否有权限访问[请求]的资源
 * @author wangjg
 * @deprecated
 */
public class RequestValidatorImpl implements RequestValidator {
	
	private ResourceFactory resourceFactory;

	public RequestValidatorImpl() {
	}

	public boolean validate(Object request, Object user) {
		Iterator it = this.resourceFactory.iterator();
		int matchedNum=0;
		while (it.hasNext()) {
			Object res = it.next();
			if(res instanceof HttpResource){
				HttpResource resource = (HttpResource)res;
				if (resource.match(request)) {
					matchedNum++;
					if(resource.validate(user)){
						return true;
					}
				}
			}else if(res instanceof Resource){
				Resource resource = (Resource)res;
				if (resource.match(request)) {
					matchedNum++;
					if(resource.validate(user)){
						return true;
					}
				}
			}
		}
		//没有定义资源，则放行
		if(matchedNum==0){
			return true;
		}
		return false;
	}

	public ResourceFactory getResourceFactory() {
		return resourceFactory;
	}

	public void setResourceFactory(ResourceFactory resourceFactory) {
		this.resourceFactory = resourceFactory;
	}
	
}
